<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8" %>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="a4j" uri="http://richfaces.org/a4j" %>
<%@taglib prefix="rich" uri="http://richfaces.org/rich" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Goodwill - Возвраты на складе</title>
    <link rel="stylesheet" href="resources/css/main.css"/>
    <link rel="stylesheet" href="resources/css/stylesheet.css"/>
    <link rel="stylesheet" type="text/css" href="resources/css/jquery.countdown.css">
    <script type="text/javascript">
        function reloadPage() {
            window.location.reload();
        }
    </script>
</head>
<body>
<f:view>
<jsp:include page="StorageMenu.jsp"/>
    <a4j:form id="jsForm" prependId="false">
        <a4j:jsFunction name="refresh" action="#{StorageEnteranceBean.reloadData}" limitToList="true"
                        reRender="goodsTable, form"/>
    </a4j:form>
    <h:form>
        <h:panelGroup layout="block" styleClass="filter_subpanel">
            <h:outputText value="Клиент" styleClass="b"/>
            <h:selectOneMenu value="#{StorageReturnsBean.storageReturnsFilter.clientId}">
                <f:selectItems value="#{StorageReturnsBean.allClientsAsSelectItems}"/>
            </h:selectOneMenu>
        </h:panelGroup>

        <h:panelGroup layout="block" styleClass="filter_subpanel">
            <h:outputText value="Артикул" styleClass="b"/>
            <h:inputText value="#{StorageReturnsBean.storageReturnsFilter.goodCode}"/>
        </h:panelGroup>

        <h:panelGroup layout="block" styleClass="filter_subpanel">
            <h:outputText value="Начало периода" styleClass="b"/>
            <rich:calendar value="#{StorageReturnsBean.storageReturnsFilter.beginDate}"/>
        </h:panelGroup>

        <h:panelGroup layout="block" styleClass="filter_subpanel">
            <h:outputText value="Конец периода" styleClass="b"/>
            <rich:calendar value="#{StorageReturnsBean.storageReturnsFilter.endDate}"/>
        </h:panelGroup>

        <a4j:commandButton reRender="storageReturnsTable" value="Применить" action="#{StorageReturnsBean.applyFilter}"/>
    </h:form>
    <a4j:form>
    <rich:dataTable id="storageReturnsTable"
                    value="#{StorageReturnsBean.storageReturnModelList}"
                    var="storageReturn"
                    styleClass="full"
                    rowKeyVar="row"
                    rows="20"
                    >
        <f:facet name="header">
            <h:outputText value="Возвраты на складе"/>
        </f:facet>

        <rich:column sortable="true" sortBy="#{storageReturn.goodCode}">
            <f:facet name="header">
                <h:outputText value="Код товара"/>
            </f:facet>
            <h:outputText value="#{storageReturn.goodCode}"/>
        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="Количество"/>
            </f:facet>
            <h:outputText value="#{storageReturn.goodCount}"/>
        </rich:column>

        <rich:column sortable="true" sortBy="#{storageReturn.companyName}">
            <f:facet name="header">
                <h:outputText value="Название компании"/>
            </f:facet>
            <h:outputText value="#{storageReturn.companyName}"/>
        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="Менеджер"/>
            </f:facet>
            <h:outputText value="#{storageReturn.managerName}"/>
        </rich:column>

        <rich:column sortable="true" sortBy="#{storageReturn.creationDate}">
            <f:facet name="header">
                <h:outputText value="Дата создания"/>
            </f:facet>
            <h:outputText value="#{storageReturn.creationDate}">
                <f:converter converterId="TimestampConverter" />
            </h:outputText>
        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="Причины"/>
            </f:facet>
            <h:outputText value="#{storageReturn.returnCause}"/>
        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="Комментарий"/>
            </f:facet>
            <h:outputText value="#{storageReturn.comment}"/>
        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="#"/>
            </f:facet>
            <a4j:commandLink value="Подтвердить" action="#{StorageReturnsBean.confirm}" oncomplete="alert('Возрат подтвержден');">
                <f:setPropertyActionListener value="#{storageReturn}" target="#{StorageReturnsBean.storageReturnModel}"/>
            </a4j:commandLink>
        </rich:column>

        <f:facet name="footer">
            <rich:datascroller id="datascroller"/>
        </f:facet>
    </rich:dataTable>
    </a4j:form>
    <a4j:commandLink value="Создать..." onclick="Richfaces.showModalPanel('create')"/>

    <rich:modalPanel id="create" autosized="true">
        <f:facet name="header">
            <h:outputText value="Добавить позицию возврата"/>
        </f:facet>
        <h:form>
            <jsp:include page="messages.jsp"/>
            <h:panelGrid columns="2" columnClasses="dleft, dright">
                <h:outputText value="Код товара"/>
                <h:inputText value="#{StorageReturnsBean.storageReturnModel.goodCode}"
                             required="true"
                             requiredMessage="Поле \"Код товара\" обязательно для заполнения"/>

                <h:outputText value="Количество"/>
                <h:inputText value="#{StorageReturnsBean.storageReturnModel.goodCount}"
                             required="true"
                             requiredMessage="Поле \"Количество\" обязательно для заполнения"
                             converterMessage="Поле \"Количество\" должно быть числом"
                             validatorMessage="Количество должно быть больше 0">
                    <f:convertNumber integerOnly="true" type="number" />
                    <f:validateLongRange minimum="1"/>
                </h:inputText>

                <h:outputText value="Компания"/>
                <h:selectOneMenu value="#{StorageReturnsBean.storageReturnModel.clientId}">
                    <f:selectItems value="#{StorageReturnsBean.allClientsAsSelectItems}"/>
                </h:selectOneMenu>

                <h:outputText value="Причина возврата"/>
                <h:inputTextarea value="#{StorageReturnsBean.storageReturnModel.returnCause}"
                                 required="true"
                                 requiredMessage="Поле \"Причина возврата\" обязательно для заполнения"/>

                <h:outputText value="Коментарий"/>
                <h:inputTextarea value="#{StorageReturnsBean.storageReturnModel.comment}"
                                 required="true"
                                 requiredMessage="Поле \"Коментарий\" обязательно для заполнения"/>
            </h:panelGrid>
            <h:panelGrid columns="2" columnClasses="butLeft,butRight">
                <a4j:commandButton value="Сохранить"
                                   action="#{StorageReturnsBean.createStorageReturn}"
                                   oncomplete="if(#{!StorageEnteranceBean.hasFacesErrors}){refresh();Richfaces.hideModalPanel('create');}"
                                   reRender="storageReturnsTable"
                        />
                <a4j:commandButton value="Отмена" onclick="refresh();" oncomplete="Richfaces.hideModalPanel('create');"/>
            </h:panelGrid>
        </h:form>
    </rich:modalPanel>

</f:view>
</body>
</html>
